/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.apps.authenticator;

import org.mockito.Mock;

import android.test.ActivityInstrumentationTestCase2;

import com.google.android.apps.authenticator.dataimport.ImportController;
import com.google.android.apps.authenticator.testability.DependencyInjector;

/**
 * Unit test for Change Application Password activity.
 *
 * @author lenharo@gmail.com (Marcos Lenharo)
 */
public class ChangeApplicationPasswordActivityTest extends
    ActivityInstrumentationTestCase2<ChangeApplicationPasswordActivity> {

  private static final String DATABASE_PASSWORD="asd123";
  private static final String DATABASE_PASSWORD_SENDKEY="A S D 1 2 3";
  private static final String DATABASE_PASSWORD2="bcd123";
  private static final String DATABASE_PASSWORD_SENDKEY2="B C D 1 2 3";
  private AccountDb mAccountDb;
  @Mock private ImportController mMockDataImportController;

  public ChangeApplicationPasswordActivityTest() {
    super(TestUtilities.APP_PACKAGE_NAME, ChangeApplicationPasswordActivity.class);
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();

    DependencyInjector.resetForIntegrationTesting(getInstrumentation().getTargetContext());
    mAccountDb = DependencyInjector.getAccountDb();
    mAccountDb.savePassword(DATABASE_PASSWORD);

    DependencyInjector.setDataImportController(mMockDataImportController);
    TestUtilities.withLaunchPreventingStartActivityListenerInDependencyResolver();
  }

  @Override
  protected void tearDown() throws Exception {
    DependencyInjector.close();

    super.tearDown();
  }

  public void testChangeApplicationPassword() {
    getActivity();
    assertTrue(mAccountDb.isEncrypted());
    sendKeys(DATABASE_PASSWORD_SENDKEY);
    sendKeys("DPAD_DOWN");
    enterPassword(DATABASE_PASSWORD_SENDKEY2, true);
    assertTrue(mAccountDb.isEncrypted());
    assertTrue(mAccountDb.setPassword(DATABASE_PASSWORD2));
  }

  public void testChangeApplicationPasswordWrongCurrentPassword() {
    getActivity();
    assertTrue(mAccountDb.isEncrypted());
    sendKeys("A");
    sendKeys("DPAD_DOWN");
    enterPassword(DATABASE_PASSWORD_SENDKEY2, true);
    assertTrue(mAccountDb.isEncrypted());
    assertTrue(mAccountDb.setPassword(DATABASE_PASSWORD));
  }

  public void testChangeApplicationPasswordWrongNewPassword() {
    getActivity();
    assertTrue(mAccountDb.isEncrypted());
    sendKeys(DATABASE_PASSWORD_SENDKEY);
    sendKeys("DPAD_DOWN");
    sendKeys(DATABASE_PASSWORD_SENDKEY);
    sendKeys("DPAD_DOWN");
    sendKeys(DATABASE_PASSWORD_SENDKEY2);
    sendKeys("DPAD_DOWN");
    sendKeys("DPAD_CENTER");
    assertTrue(mAccountDb.isEncrypted());
    assertTrue(mAccountDb.setPassword(DATABASE_PASSWORD));
  }
  
  private void enterPassword(String password, boolean confirmation) {
    sendKeys(password);
    sendKeys("DPAD_DOWN");
    if (confirmation) {
      sendKeys(password);
      sendKeys("DPAD_DOWN");
    }
    sendKeys("DPAD_CENTER");
  }
}
